package krautz.fitness;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends ListActivity {

	private ListView list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ArrayList<String> listItems = new ArrayList<String>();

		/*
		 * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		 * android.R.layout.simple_list_item_1, listItems);
		 * setListAdapter(adapter);
		 */
		IconArrayAdapter adapter = new IconArrayAdapter(this, R.layout.main,listItems);
		setListAdapter(adapter);

		FoodDatabase fd = new FoodDatabase(this);

		try {
			fd.createDatabase();
		} catch (IOException e) {
			e.printStackTrace();
		}

		fd.openDataBase();
		Cursor c = fd.query("select distinct restaurant from FoodItems order by restaurant", null);

		while (c.moveToNext()) {
			listItems.add(c.getString(0));
		}

	}

	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Object o = this.getListAdapter().getItem(position);
		String restaurant = o.toString();
		Intent foodView = new Intent(Main.this, FoodItemList.class);
		Bundle bundle = new Bundle();
		bundle.putString("restaurant", restaurant);
		foodView.putExtras(bundle);
		startActivity(foodView);
	}

	/* Private class for overriding the array adapter to put icons in rows */
	private class IconArrayAdapter extends ArrayAdapter<String> {
		private List<String> items;
		private LayoutInflater inflater;

		public IconArrayAdapter(Context context, int textViewResourceId,
				List<String> items) {
			super(context, textViewResourceId, items);

			this.items = items;
			this.inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View vi = convertView;
			if (convertView == null)
				vi = inflater.inflate(R.layout.restaurantitem, null);

			TextView text = (TextView) vi.findViewById(R.id.text);
			ImageView image = (ImageView) vi.findViewById(R.id.image);

			image.setImageResource(getResourceIdForIcon(items.get(position)));
			text.setText(items.get(position));
			// text.setText("item "+position);
			// imageLoader.DisplayImage(data[position], activity, image);
			return vi;
		}
		
		/**
		 * Returns the resource id of the given restaurant string
		 * @param restaurant
		 * @return resource id of image
		 */
		private int getResourceIdForIcon(String restaurant) {
			if (restaurant.equals("subway"))
				return R.drawable.subway;
			else if(restaurant.equals("mcdonalds"))
				return R.drawable.mcdonalds;
			else
				return R.drawable.icon;
		}
	}

}